package org.appchat.service.protobuf.integration;

import com.google.protobuf.Message;
import org.appchat.api.Listener;
import org.appchat.service.ChatRoom;
import org.appchat.service.protobuf.Test;

import java.util.concurrent.Callable;

import static com.jayway.awaitility.Awaitility.*;

/**
 * Created by Ryan of app-chat.org
 * on: 7/14/12 at 5:47 PM
 */
public class Integration {

    private boolean messageReceived = false;

    @org.junit.Test
    public void testCanAddListenerAndReceiveMessage() throws Exception{


        Message protobufMessage = Test.SearchRequest.newBuilder().setTest("protowin").build();

        ChatRoom chatRoom = ChatRoom.builder().listenFor(new TestingListener()).build();
        chatRoom.startChat();

        chatRoom.getPublisher().publish(protobufMessage);

        await().until(messageIsReceived());


    }

    private Callable<Boolean> messageIsReceived() {
        return new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return messageReceived;
            }
        };
    }

    private class TestingListener implements Listener<Test.SearchRequest> {

        @Override
        public void messageReceived(Test.SearchRequest message) {
            System.out.println("Message Received");
            messageReceived = true;
        }

        @Override
        public String getQueueName() {
            return Test.SearchRequest.getDescriptor().getFullName();
        }
    }

}
